package com.clps.dev.sms.service.sm.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.clps.dev.sms.common.result.ResponseResult;
import com.clps.dev.sms.common.result.ResultType;
import com.clps.dev.sms.common.service.impl.BaseServiceImpl;
import com.clps.dev.sms.dao.sm.DepartmentDao;
import com.clps.dev.sms.entity.sm.Department;
import com.clps.dev.sms.service.sm.IDepartmentService;
import com.clps.dev.sms.util.excel.ExcelTool;
import com.clps.dev.sms.util.result.ResponseResultUtil;

/**  
* @ClassName: DepartmentServiceImpl  
* @Description: DepartmentServiceImpl  
* @since JDK 1.8 
* @author RuiShan.Yang  
* @date 2018-11-28 13:07:40  
* 
*/
@Service
@Transactional
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

	/**  
	* 创建一个新的实例 DepartmentServiceImpl.  
	*/
	public DepartmentServiceImpl() {
		super(Department.class);

	}

	@Autowired
	private DepartmentDao departmentDao;

	/**
	* <p>Title: exportDepartmentByExcel</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.service.sm.IDepartmentService#exportDepartmentByExcel(java.util.List, java.lang.String)  
	*/
	@Override
	public ResponseResult<Department> exportDepartmentByExcel(List<Department> departmentList, String excelPath) {

		ExcelTool.exportExcel(departmentList, excelPath);
		return null;

	}

	/**
	* <p>Title: addDepartment</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.service.sm.IDepartmentService#addDepartment(com.clps.dev.sms.entity.sm.Department)  
	*/
	@Override
	public ResponseResult<Department> addDepartment(Department department) {
		String resVal = departmentDao.save(department);
		if (ResultType.ADD_SUCCESS.equals(resVal)) {
			return ResponseResultUtil.success(resVal);
		}
		return ResponseResultUtil.error(resVal);
	}

	/**
	* <p>Title: editDepartment</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.service.sm.IDepartmentService#editDepartment(com.clps.dev.sms.entity.sm.Department)  
	*/
	@Override
	public ResponseResult<Department> editDepartment(Department department) {
		String resVal = departmentDao.update(department, department.getDepartmentId());
		if (ResultType.UPDATE_SUCCESS.equals(resVal)) {
			return ResponseResultUtil.success(resVal);
		}
		return ResponseResultUtil.error(resVal);

	}

	/**
	* <p>Title: findALlDepartment</p>  
	* <p>Description: </p>  
	* @return  
	* @see com.clps.dev.sms.service.sm.IDepartmentService#queryAllDepartment()  
	*/
	@Override
	public ResponseResult<Department> queryAllDepartment() {

		List<Department> departmentList = departmentDao.queryALl();
		if (departmentList.size() > 0) {
			return ResponseResultUtil.success(ResultType.QUERY_SUCCESS, departmentList);
		}
		return ResponseResultUtil.error(ResultType.QUERY_FAULT);
	}

	/**
	* <p>Title: queryDepartmentBySql</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.service.sm.IDepartmentService#queryDepartmentBySql(java.lang.String)  
	*/
	@Override
	public ResponseResult<Department> queryDepartmentBySql(String sql) {
		List<Department> departmentList = departmentDao.queryBySql(sql);
		if (departmentList.size() > 0) {
			return ResponseResultUtil.success(ResultType.QUERY_SUCCESS, departmentList);
		}
		return ResponseResultUtil.error(ResultType.QUERY_FAULT);
	}

	@Override
	public ResponseResult<Department> queryDepartmentByLikeName(String departmentName) {
		List<Department> departmentList = departmentDao.queryByDepartmentLikeName(departmentName);
		if (departmentList.size() > 0) {
			return ResponseResultUtil.success(ResultType.QUERY_SUCCESS, departmentList);
		}
		return ResponseResultUtil.error(ResultType.QUERY_FAULT);
	}

	/**
	* <p>Title: isUniqueByField</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.service.sm.IDepartmentService#isUniqueByField(java.lang.String, java.lang.String)  
	*/
	@Override
	public ResponseResult<Department> isUniqueByField(String departmentFieldName, String departmentFieldVal) {
		List<Department> departmentList = departmentDao.queryByField(departmentFieldName, departmentFieldVal);
		if (departmentList.size() > 0) {
			return ResponseResultUtil.error(ResultType.CONTENT_REPEAT);
		}
		return ResponseResultUtil.success(ResultType.CONTENT_NOT_REPEAT);
	}

	/**
	* <p>Title: deleteDepartmentById</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.service.sm.IDepartmentService#deleteDepartmentById(java.lang.Integer)  
	*/
	@Override
	public ResponseResult<Department> deleteDepartmentById(Integer departmentId) {

		String resVal = departmentDao.deleteById(departmentId);
		if (ResultType.DELETE_SUCCESS.equals(resVal)) {
			return ResponseResultUtil.success(resVal);
		}
		return ResponseResultUtil.error(resVal);
	}

	/**
	* <p>Title: queryDepartmentById</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.service.sm.IDepartmentService#queryDepartmentById(java.lang.Integer)  
	*/
	@Override
	public ResponseResult<Department> queryDepartmentById(Integer departmentId) {
		Department department = departmentDao.queryById(departmentId);
		List<Department> departments = new ArrayList<>();
		departments.add(department);
		if (null != department) {
			return ResponseResultUtil.success(ResultType.QUERY_SUCCESS, departments);
		}
		return ResponseResultUtil.error(ResultType.QUERY_FAULT);
	}

}
